package com.g3d.helpers;

public class G3DTickTimer 
{

	private long _currenTime = 0;
	private boolean _isEnabled = false;
	private boolean _loop = false;
	private int _interval = 0;
	
	public G3DTickTimer(int interval)
	{
		this(interval, false, false);
	}
	public G3DTickTimer(int interval, boolean enabled)
	{
		this(interval, enabled, false);
	}
	public G3DTickTimer(int interval, boolean enabled, boolean loop)
	{
		this._interval = interval;
		this._isEnabled = enabled;
		this._loop = loop;
	}
	
	public boolean isEnabled()
	{
		return this._isEnabled;
	}
	public void isEnabled(boolean val)
	{
		this._isEnabled = val;
	}
	
	public boolean loop()
	{
		return this._loop;
	}
	public void loop(boolean val)
	{
		this._loop = val;
	}
	
	public int intervalMilliseconds()
	{
		return this._interval;
	}
	public void intervalMilliseconds(int val)
	{
		this._interval = val;
	}
	
	public int intervalSeconds()
	{
		return (this._interval / 1000);
	}
	public void intervalSeconds(int val)
	{
		this._interval = (val* 1000);
	}
	
	public int elapsedMilliseconds()
	{
		return (int)(System.currentTimeMillis() - this._currenTime);
	}
	public int elapsedSeconds()
	{
		return (elapsedMilliseconds() / 1000);
	}
	
	public int leftMilliseconds()
	{
		return (int)((this._currenTime + this._interval) - System.currentTimeMillis());
	}
	public int leftSeconds()
	{
		return (leftMilliseconds() / 1000);
	}
	
	public boolean tick()
	{
		if(this._isEnabled)
		{
			long currentMilisec = System.currentTimeMillis();
			if(this._currenTime == 0)
			{
				this._currenTime = currentMilisec;
			}
			else
			{
				if((this._currenTime - currentMilisec) >= this._interval)
				{
					this._currenTime = currentMilisec;
					this._isEnabled = this._loop;
					return true;
				}
			}
		}
		return false;
	}

	public void reset()
	{
		this.reset(this._isEnabled, this._loop);
	}
	public void reset(boolean enabled)
	{
		this.reset(enabled, this._loop);
	}
	public void reset(boolean enabled, boolean loop)
	{
		this._currenTime = System.currentTimeMillis();
		this._isEnabled = enabled;
		this._loop = loop;
	}
}
